﻿Partial Public Class InventoryBatchExplorer

    Public aLotDataRow As DataRow
    Public aLotStockDataRow As DataRow
    Public aProductDataRow As DataRow

    Public Sub DeleteRow(ByVal row As DataRow, ByVal e As CancelEventArgs)
        Dim aDt As DataTable

        If row IsNot Nothing Then
            Select Case row.Table.TableName
                Case "InventoryBatchs"
                    If row.Table.DataSet IsNot Nothing Then
                        If Not IsDBNull(row("StatusID_FK")) AndAlso (DirectCast(row("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_REL Or DirectCast(row("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_TRANS) Then
                            MessageWindow.ShowMessage("You Cannot delete a batch with Status: Released or Transmitted", MsgBoxStyle.Information)
                            e.Cancel = True
                        Else
                            aDt = row.Table.DataSet.Tables("InventoryBatchsTransaction")
                            If aDt.Rows.Count > 0 Then
                                MessageWindow.ShowMessage("Please Delete all the transaction line, Delete abord", MsgBoxStyle.Information)
                                e.Cancel = True
                            End If
                        End If
                    End If
                Case "InventoryBatchsTransaction"
                    If Not IsDBNull(row("StatusID_FK")) AndAlso DirectCast(row("StatusID_FK"), Guid) <> IdRepository.APP_TRANSTATUS_OPEN Then
                        MessageWindow.ShowMessage("Only Open Transaction Can Be Deleted", MsgBoxStyle.Information)
                        e.Cancel = True
                    End If

                Case Else
            End Select
        End If
    End Sub

    Public Overridable Sub InitGridActionOption(ByVal aGrid As GridExtPL)
        Dim action As ActionOption
        'HOWTO : Howto Translate...
        Dim NotRelease As String = DeskTopManager.GetMessage("CONNECT")

        action = New ActionOption("Process", "ACT_00070", "Process Batch (WSvc)", AddressOf ProcessBatch, True, True)
        aGrid.GridActionOptionList.Add(action)
        action = New ActionOption("Process", "ACT_00071", "Process Batch Transaction (WSvc)", AddressOf aLotTranHelper.ProcessBatchTransaction, True, True, "InventoryBatchsTransaction")
        aGrid.GridActionOptionList.Add(action)
        action = New ActionOption("Debug", "ACT_00072", "DUMP XML", AddressOf DumpFile, True, True)
        aGrid.GridActionOptionList.Add(action)
    End Sub

    '
    Public Overridable Sub MasterRowClick(ByVal sender As Object, ByVal e As RowClickEventArgs)
        Dim aView As GridView = DirectCast(sender, GridView)
        Dim aRow As DataRow = aView.GetFocusedDataRow()


        'Approch: We start with the position of focus then from the grid by design behevior
        'we can evaluate the state of a new row or an existing row to lock or unlock and validate the state as per Factory context.
        'On filter row we unlock and step out.

        If aView.FocusedRowHandle = GridControl.AutoFilterRowHandle Or aView.FocusedRowHandle = GridControl.NewItemRowHandle Then
            UnLockColumns(aView)
        ElseIf aRow IsNot Nothing Then
            If Not IsDBNull(aRow("StatusID_FK")) AndAlso (DirectCast(aRow("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_REL Or DirectCast(aRow("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_TRANS) Then  'Factoring possibility
                LockColumns(aView)
            Else
                UnLockColumns(aView)
            End If
        End If
    End Sub
    '
    Public Overridable Sub DetailRowClick(ByVal sender As Object, ByVal e As RowClickEventArgs)
        Dim aView As GridView = DirectCast(sender, GridView)
        Dim aRow As DataRow = aView.GetFocusedDataRow()

        If aView.FocusedRowHandle = GridControl.AutoFilterRowHandle Then
            UnLockColumns(aView)
        ElseIf aRow IsNot Nothing Then
            AssignViewRowProperty(aView, aRow)
        Else
            LockColumns(aView)
            aView.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        End If
    End Sub
    '
    Public Overridable Sub ValidateMasterRow(ByVal sender As Object, ByVal e As Views.Base.ValidateRowEventArgs)
        Dim row As DataRow = DirectCast(e.Row, DataRowView).Row
        If row IsNot Nothing Then
            row.ClearErrors()
            Using invBatch As SystemBoxSDK.PL.INVSDK.InventoryBatchs_PL = New SystemBoxSDK.PL.INVSDK.InventoryBatchs_PL()
                invBatch.ValidateRow(row)
            End Using
        End If
    End Sub
    '
    Public Overridable Sub ValidateDetailRow(ByVal sender As Object, ByVal e As Views.Base.ValidateRowEventArgs)
        Dim row As DataRow = DirectCast(e.Row, DataRowView).Row
        If row IsNot Nothing Then
            Select Case row.Table.TableName
                Case "InventoryBatchsTransaction"
                    Using lotTran As SystemBoxSDK.PL.MESSDK.LotTransaction_PL = New SystemBoxSDK.PL.MESSDK.LotTransaction_PL()
                        lotTran.ValidateRow(row)
                    End Using
                Case Else
            End Select
        End If
    End Sub

    Public Overridable Sub InitNewMasterRow(ByVal sender As Object, ByVal e As InitNewRowEventArgs)
        Dim view As GridView = DirectCast(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        Using inventoryBatchs As SystemBoxSDK.PL.INVSDK.InventoryBatchs_PL = New SystemBoxSDK.PL.INVSDK.InventoryBatchs_PL()
            inventoryBatchs.NewRecord(row)
        End Using
    End Sub

    Public Overridable Sub InitNewDetailRow(ByVal sender As Object, ByVal e As InitNewRowEventArgs)
        Dim view As GridView = DirectCast(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        If row.Table.TableName = "InventoryBatchsTransaction" Then ' this is the MetaqueryName
            Using lotTran As SystemBoxSDK.PL.MESSDK.LotTransaction_PL = New SystemBoxSDK.PL.MESSDK.LotTransaction_PL()
                lotTran.NewRecord(row)
            End Using
        End If
    End Sub

    Public Overridable Sub ProcessCurrentDetailRecord(ByVal sender As Object, ByVal e As Views.Base.ValidateRowEventArgs, ByVal index As Integer)
        Dim row As DataRow = DirectCast(e.Row, DataRowView).Row
        If row IsNot Nothing Then
            '
            'Using lotTran As New SystemBoxSDK.PL.MESSDK.LotTransaction_PL()
            Select Case row.Table.TableName
                Case "InventoryBatchsTransaction"
                    '
                    ' On ne peut pas modifier de transaction release ou Transmitted
                    If Not IsDBNull(row("StatusID_FK")) AndAlso (DirectCast(row("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_REL Or DirectCast(row("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_TRANS) Then
                        Return
                    End If
                    '
                    If Not IsDBNull(row("TranTypeID_FK")) Then
                        Select Case DirectCast(row("TranTypeID_FK"), Guid)
                            Case IdRepository.APP_TRAN_ADJMNT, IdRepository.APP_TRAN_RTRNTOSTOCK
                                AssignDefaultValueForAdjustement(row)
                            Case IdRepository.APP_TRAN_PHYCNT
                                AssignDefaultValueForPhysCount(row)
                            Case IdRepository.APP_TRAN_PHYCNTINV
                                AssignDefaultValueForPhysInv(row)
                            Case IdRepository.APP_TRAN_RCPT
                                AssignDefaultValueForRcpt(row)
                            Case IdRepository.APP_TRAN_TRNFER
                                AssignDefaultValueForTrsf(row)
                            Case IdRepository.APP_TRAN_WTRAN
                                AssignDefaultValueForWhseTrsf(row)
                            Case IdRepository.APP_TRAN_SPLIT
                                AssignDefaultValueForSplit(row)
                            Case IdRepository.APP_TRAN_SALES
                                AssignDefaultValueForSales(row)
                            Case Else
                        End Select
                    End If
            End Select
            ' End Using
        End If
    End Sub


    Public Overridable Sub DetailCustomRowCellEditForEditing(ByVal sender As Object, ByVal e As CustomRowCellEditEventArgs)
        Dim aDetailview As GridView = DirectCast(sender, GridView)
        Dim aLotTranRow As DataRow = aDetailview.GetFocusedDataRow()
        Dim aEdit As RepositoryItemGridLookUpEdit
        Dim aLotId As Guid
        Dim sqlWhere As String
        If aLotTranRow Is Nothing Then
            'Sécurtité du LOV selon l'Acces de l'utilisateur
            If e.Column.FieldName = "TranTypeID_FK" Then
                sqlWhere = String.Format("ListValueId IN ({0})", AppManager.GetGCCTSSecurity(IdRepository.ALLOWCREATE, IdRepository.APP_TRAN))

                aEdit = LOVBuilder("LOV_TranType", sqlWhere, Nothing, Nothing)
                aEdit.Name = "LOV_TranType"
                aDetailview.GridControl.RepositoryItems.Add(aEdit)
                e.RepositoryItem = aEdit
            End If
        Else
            Select Case aLotTranRow.Table.TableName
                Case "InventoryBatchsTransaction"

                    'Sécurtité du LOV selon l'Acces de l'utilisateur en Fonction des GCCTS
                    '---------To refactor with SSP LinQ ---------'
                    If e.Column.FieldName = "TranTypeID_FK" Then
                        sqlWhere = String.Format("ListValueId IN ({0})", AppManager.GetGCCTSSecurity(IdRepository.ALLOWUPDATE, IdRepository.APP_TRAN))

                        aEdit = LOVBuilder("LOV_TranType", sqlWhere, Nothing, Nothing)
                        aEdit.Name = "LOV_TranType"
                        aDetailview.GridControl.RepositoryItems.Add(aEdit)
                        e.RepositoryItem = aEdit
                    End If
                    '---------END refactor ---------'


                    If Not IsDBNull(aLotTranRow("TranTypeID_FK")) Then
                        Select Case DirectCast(aLotTranRow("TranTypeID_FK"), Guid)
                            ' la section des ajustements
                            Case IdRepository.APP_TRAN_ADJMNT, IdRepository.APP_TRAN_PHYCNT, IdRepository.APP_TRAN_PHYCNTINV

                                If e.Column.FieldName = "FROMWhBinID_FK" Then
                                    aLotId = CType(If(IsDBNull(aLotTranRow("LotID_FK")), Guid.Empty, aLotTranRow("LotID_FK")), Guid)

                                    'A garder si on doit enlever le SCOPING des Bin
                                    'sqlWhere = ""
                                    'aEdit = LOVBuilder("LOV_SiteSection", sqlWhere, Nothing, Nothing)
                                    'aEdit = LOVBuilder("LOV_SiteSection", sqlWhere, Nothing, Nothing)

                                    sqlWhere = String.Format("LotStock.LotID_FK = '{0}'", aLotId.ToString)
                                    aEdit = LOVBuilder("LOV_SiteSectionWithStock", sqlWhere, Nothing, Nothing)

                                    aEdit.Name = "LOV_SiteSectionWithStock"
                                    aDetailview.GridControl.RepositoryItems.Add(aEdit)
                                    e.RepositoryItem = aEdit
                                End If

                                If e.Column.FieldName = "TOWhBinID_FK" Then
                                    'A garder si on doit enlever le SCOPING des Bin
                                    'sqlWhere = ""

                                    sqlWhere = String.Format("Site.SiteId = '{0}'", IdRepository.APP_SITE_ADJUST.ToString)
                                    aEdit = LOVBuilder("LOV_SiteSection", sqlWhere, Nothing, Nothing)
                                    aEdit.Name = "LOV_SiteSection"


                                    aDetailview.GridControl.RepositoryItems.Add(aEdit)
                                    e.RepositoryItem = aEdit
                                End If

                            Case IdRepository.APP_TRAN_RCPT

                                If e.Column.FieldName = "TOWhBinID_FK" Then
                                    sqlWhere = String.Format("Site.SiteId <> '{0}'", IdRepository.APP_SITE_ADJUST.ToString)
                                    aEdit = LOVBuilder("LOV_SiteSection", sqlWhere, Nothing, Nothing)
                                    aEdit.Name = "LOV_SiteSection"
                                    aDetailview.GridControl.RepositoryItems.Add(aEdit)
                                    e.RepositoryItem = aEdit
                                End If

                            Case IdRepository.APP_TRAN_TRNFER, IdRepository.APP_TRAN_SPLIT

                                If e.Column.FieldName = "FROMWhBinID_FK" Then
                                    aLotId = CType(If(IsDBNull(aLotTranRow("LotID_FK")), Guid.Empty, aLotTranRow("LotID_FK")), Guid)
                                    sqlWhere = String.Format("LotStock.LotID_FK = '{0}' AND Site.SiteId <> '{1}'", aLotId.ToString, IdRepository.APP_SITE_ADJUST.ToString)
                                    aEdit = LOVBuilder("LOV_SiteSectionWithStock", sqlWhere, Nothing, Nothing)
                                    aEdit.Name = "LOV_SiteSectionWithStock"
                                    aDetailview.GridControl.RepositoryItems.Add(aEdit)
                                    e.RepositoryItem = aEdit
                                End If

                                If e.Column.FieldName = "TOWhBinID_FK" Then
                                    sqlWhere = String.Format("Site.SiteId <> '{0}'", IdRepository.APP_SITE_ADJUST.ToString)
                                    aEdit = LOVBuilder("LOV_SiteSection", sqlWhere, Nothing, Nothing)
                                    aEdit.Name = "LOV_SiteSection"
                                    aDetailview.GridControl.RepositoryItems.Add(aEdit)
                                    e.RepositoryItem = aEdit
                                End If
                            Case IdRepository.APP_TRAN_SALES
                                If e.Column.FieldName = "FROMWhBinID_FK" Then
                                    aLotId = CType(If(IsDBNull(aLotTranRow("LotID_FK")), Guid.Empty, aLotTranRow("LotID_FK")), Guid)
                                    sqlWhere = String.Format("LotStock.LotID_FK = '{0}' AND Site.SiteId <> '{1}' AND LotStock.QtyOnHand > 0.0", aLotId.ToString, IdRepository.APP_SITE_ADJUST.ToString)
                                    aEdit = LOVBuilder("LOV_SiteSectionWithStock", sqlWhere, Nothing, Nothing)
                                    aEdit.Name = "LOV_SiteSectionWithStock"
                                    aDetailview.GridControl.RepositoryItems.Add(aEdit)
                                    e.RepositoryItem = aEdit
                                End If
                            Case Else
                        End Select
                    End If
            End Select
        End If
    End Sub
    '
    Public Overridable Sub DumpFile(ByVal sender As Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim mview As MetaDataView

        Dim invBatchID As Guid = New Guid(aDataRow("InvBatchID").ToString)
        '
        mview = New MetaDataView("LotTranToLEWIS")
        mview.GetMetaCommand()
        mview.MetaCommand.SQLStatement = String.Format(mview.MetaCommand.SQLStatement, "WHERE LotTransaction.InvBatchID_FK = '" + invBatchID.ToString + "'")
        mview.FillView()
        mview.View.WriteXml("e:\AndreINV.xml")
        '
    End Sub

    Public Overridable Sub ProcessBatch(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        Dim SysBoxSvc As SystemBoxWSvc = New SystemBoxWSvc()
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Try
            SysBoxSvc.ProcessBatch(aDataRow)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub
    '
    Public Overridable Sub DetailCellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Dim view As GridView = DirectCast(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()

        If row IsNot Nothing Then
            If row.Table.TableName = "InventoryBatchsTransaction" Then
                If e.Column.FieldName = "TranTypeID_FK" Then
                    InitRow(row)
                    row("LotID_FK") = DBNull.Value
                    row("PrdId_FK") = DBNull.Value
                    AssignDefaultValue(row)
                    AssignViewRowProperty(view, row)
                End If
                If e.Column.FieldName = "PrdId_FK" Then
                    InitRow(row)
                    row("LotID_FK") = DBNull.Value
                    AssignDefaultValue(row)
                    AssignViewRowProperty(view, row)
                End If
                If e.Column.FieldName = "LotID_FK" Then
                    InitRow(row)
                    row("PrdId_FK") = DBNull.Value
                    AssignDefaultValue(row)
                    AssignViewRowProperty(view, row)
                End If

                If e.Column.FieldName = "FROMWhBinID_FK" Then
                    AssignDefaultValue(row)
                    AssignViewRowProperty(view, row)
                End If

            End If
        End If
    End Sub
    '
    Public Overridable Sub AssignViewRowProperty(ByVal view As GridView, ByVal row As DataRow)

        Dim dataView As DataView = CType(view.DataSource, DataView)

        Dim AllowTranUpd As String = AppManager.GetGCCTSSecurity(IdRepository.ALLOWUPDATE, IdRepository.APP_TRAN).ToLower
        Dim AllowTranDel As String = AppManager.GetGCCTSSecurity(IdRepository.ALLOWDELETE, IdRepository.APP_TRAN).ToLower

        If row IsNot Nothing Then
            view.BeginUpdate()

            ' On ne peut pas modifier de transaction release ou Transmitted
            If Not IsDBNull(row("StatusID_FK")) AndAlso (DirectCast(row("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_REL Or DirectCast(row("StatusID_FK"), Guid) = IdRepository.APP_TRANSTATUS_TRANS) Then
                LockColumns(view)
            Else
                'On assigne les securités sur la ligne
                If Not IsDBNull(row("TranTypeID_FK")) Then
                    If Not AllowTranDel.Contains(row("TranTypeID_FK").ToString) Then
                        dataView.AllowDelete = False
                    Else
                        dataView.AllowDelete = True
                    End If
                    If Not AllowTranUpd.Contains(row("TranTypeID_FK").ToString) Then
                        dataView.AllowEdit = False
                        LockColumns(view)
                    Else
                        dataView.AllowEdit = True
                        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
                    End If

                    ' Selon le LotTran Type on débarre les colonnes.
                    If dataView.AllowEdit Then
                        LockColumns(view)
                        EnableReadOnly(view)
                        Select Case DirectCast(row("TranTypeID_FK"), Guid)
                            Case IdRepository.APP_TRAN_RCPT
                                PrepareColumnForReceipt(view)
                            Case IdRepository.APP_TRAN_ADJMNT
                                PrepareColumnForAdjmnt(view)
                            Case IdRepository.APP_TRAN_PHYCNT, IdRepository.APP_TRAN_PHYCNTINV
                                PrepareColumnForPhysCount(view)
                            Case IdRepository.APP_TRAN_SALES
                                PrepareColumnForSales(view)
                            Case IdRepository.APP_TRAN_TRNFER
                                PrepareColumnForTrsf(view)
                            Case IdRepository.APP_TRAN_WTRAN
                                PrepareColumnForWhseTrsf(view)
                            Case IdRepository.APP_TRAN_SPLIT
                                PrepareColumnForSplit(view)
                            Case IdRepository.APP_TRAN_RTRNTOSTOCK
                                PrepareColumnForReturnToStock(view)
                            Case Else
                        End Select
                    End If
                End If
            End If
            view.EndUpdate()
        End If
    End Sub
    '
    Public Overridable Sub PrepareColumnForReceipt(ByVal view As GridView)
        Dim aRow As DataRow = view.GetFocusedDataRow()

        'C'est Quand on descend 
        ' le validateRow a déjà Lever
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True


        If Not IsDBNull(aRow("LotID_FK")) Then
            view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
            view.Columns("LotTypeId_FK").OptionsColumn.AllowEdit = False
            view.Columns("LotCD").OptionsColumn.AllowEdit = False
            view.Columns("PrdId_FK").OptionsColumn.AllowEdit = False
        Else
            view.Columns("LotTypeId_FK").OptionsColumn.AllowEdit = True
            view.Columns("LotCD").OptionsColumn.AllowEdit = True
            view.Columns("PrdId_FK").OptionsColumn.AllowEdit = True
        End If

        If IsDBNull(aRow("PrdId_FK")) AndAlso IsDBNull(aRow("LotCD")) AndAlso IsDBNull(aRow("LotID_FK")) Then
            view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
            view.Columns("LotTypeId_FK").OptionsColumn.AllowEdit = True
            view.Columns("PrdId_FK").OptionsColumn.AllowEdit = True
            view.Columns("LotCD").OptionsColumn.AllowEdit = True
        End If

        If Not IsDBNull(aRow("PrdId_FK")) AndAlso IsDBNull(aRow("LotCD")) Then
            view.Columns("LotCD").OptionsColumn.AllowEdit = True
        End If

        If Not IsDBNull(aRow("LotCD")) AndAlso IsDBNull(aRow("PrdId_FK")) Then
            view.Columns("PrdId_FK").OptionsColumn.AllowEdit = True
        End If

        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True

        'En commentaire car on prend les storage unit du Produit
        'view.Columns("TranUnitId_FK").OptionsColumn.AllowEdit = True

        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
        '
        view.Columns("SupplierLotNo").OptionsColumn.AllowEdit = True
        view.Columns("Carrier").OptionsColumn.AllowEdit = True
        view.Columns("BillOfLading").OptionsColumn.AllowEdit = True
        view.Columns("PackingSlipNo").OptionsColumn.AllowEdit = True
        '
        view.Columns("COA").OptionsColumn.AllowEdit = True
        view.Columns("ExpiryDate").OptionsColumn.AllowEdit = True
    End Sub
    ''' <summary>
    ''' This is to prepare colums of a Return to Stock
    ''' </summary>
    ''' <param name="view"></param>
    ''' <remarks>
    ''' This transaction type is allowed only ofr SCIEF devepper and Archictech for testing in open loop
    ''' Any Group of User shall not have access directly
    ''' Only User Group of ProdOrder Form should be granted access to that Transaction type via GCCTS list
    ''' </remarks>
    Public Overridable Sub PrepareColumnForReturnToStock(view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub

    Public Overridable Sub PrepareColumnForWhseTrsf(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = False
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub

    Public Overridable Sub PrepareColumnForTrsf(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = False
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub

    Public Overridable Sub PrepareColumnForSplit(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub

    Public Overridable Sub PrepareColumnForSales(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = False
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub
    '
    Public Overridable Sub AssignDefaultValue(ByVal row As DataRow)
        row("StatusID_FK") = IdRepository.APP_TRANSTATUS_PDG
        If IsDBNull(row("TranDate")) Then
            row("TranDate") = Date.Today
        End If

        If Not IsDBNull(row("LotID_FK")) Then
            aLotDataRow = SystemBoxSDK.PL.MESSDK.Lot_PL.GetLotByID(DirectCast(row("LotID_FK"), Guid))
            If aLotDataRow IsNot Nothing Then
                row("PrdId_FK") = aLotDataRow("PrdId_FK")
                row("LotTypeId_FK") = aLotDataRow("LotTypeId_FK")
                row("FROMLotStatusId_FK") = aLotDataRow("StatusID_FK")
                row("LotCD") = aLotDataRow("LotDescr_Prm")
            End If
        End If
        '
        If Not IsDBNull(row("PrdId_FK")) Then
            aProductDataRow = SystemBoxSDK.PL.CoreSDK.Product_PL.GetProductByID(DirectCast(row("PrdId_FK"), Guid))
            If aProductDataRow IsNot Nothing Then
                row("TranUnitId_FK") = aProductDataRow("StorageUnitId_FK")
            End If
        End If

        If IsDBNull(row("Qty")) Then
            If Not IsDBNull(row("FROMWhBinID_FK")) AndAlso Not IsDBNull(row("LotID_FK")) Then
                aLotStockDataRow = SystemBoxSDK.PL.MESSDK.Lot_PL.GetLotSectionByID(DirectCast(row("LotID_FK"), Guid), DirectCast(row("FROMWhBinID_FK"), Guid))
                If aLotStockDataRow IsNot Nothing Then
                    row("Qty") = aLotStockDataRow("QtyOnHand")
                End If
            End If
        End If
    End Sub

    Public Overridable Sub AssignDefaultValueForAdjustement(ByVal row As DataRow)
        AssignDefaultValue(row)
    End Sub

    Public Overridable Sub AssignDefaultValueForRcpt(ByVal row As DataRow)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_RCPT
        AssignDefaultValue(row)
    End Sub

    Public Overridable Sub AssignDefaultValueForTrsf(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_TRNFER
    End Sub

    Public Overridable Sub AssignDefaultValueForWhseTrsf(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_WTRAN
    End Sub

    Public Overridable Sub AssignDefaultValueForSplit(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_SPLIT
    End Sub

    Public Overridable Sub AssignDefaultValueForPhysCount(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_PHYCNT
        row("TOWhBinID_FK") = IdRepository.APP_SITESECTION_ADJUST_PHYCPT
    End Sub

    Public Overridable Sub AssignDefaultValueForPhysInv(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_PHYCNTINV
        row("TOWhBinID_FK") = IdRepository.APP_SITESECTION_ADJUST_PHYCPT
    End Sub

    Public Overridable Sub AssignDefaultValueForSales(ByVal row As DataRow)
        AssignDefaultValue(row)
        row("TranTypeID_FK") = IdRepository.APP_TRAN_SALES
    End Sub

    Public Overridable Sub PrepareColumnForAdjmnt(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub
    '
    Public Overridable Sub PrepareColumnForPhysCount(ByVal view As GridView)
        view.Columns("TranTypeID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TranDate").OptionsColumn.AllowEdit = True
        view.Columns("LotID_FK").OptionsColumn.AllowEdit = True
        view.Columns("FROMWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("TOWhBinID_FK").OptionsColumn.AllowEdit = True
        view.Columns("Qty").OptionsColumn.AllowEdit = True
        view.Columns("RefDoc").OptionsColumn.AllowEdit = True
        view.Columns("Comment").OptionsColumn.AllowEdit = True
    End Sub
    '
    Public Overridable Sub LockColumns(ByVal view As GridView)
        For Each col As GridColumn In view.Columns
            col.OptionsColumn.AllowEdit = False
        Next
    End Sub

    'OutStandingIssue: Devrait rependre les valeurs de lock de la metaquery - aleblanc.
    Public Overridable Sub UnLockColumns(ByVal view As GridView)
        For Each col As GridColumn In view.Columns
            col.OptionsColumn.AllowEdit = True
        Next
    End Sub

    Public Overridable Sub EnableReadOnly(ByVal view As GridView)
        For Each col As GridColumn In view.Columns
            col.OptionsColumn.ReadOnly = False
        Next
    End Sub

    Public Overridable Sub InitRow(ByVal row As DataRow)
        row("TranDate") = DBNull.Value
        'row("PrdId_FK") = DBNull.Value
        'row("LotID_FK") = DBNull.Value
        row("LotCD") = DBNull.Value
        row("FROMLotStatusId_FK") = DBNull.Value
        row("TOLotStatusId_FK") = DBNull.Value
        row("Qty") = DBNull.Value
        row("TranUnitId_FK") = DBNull.Value
        row("StatusID_FK") = DBNull.Value
        row("Comment") = DBNull.Value
        row("LotTypeId_FK") = DBNull.Value
        row("RefDoc") = DBNull.Value
        row("FROMWhBinID_FK") = DBNull.Value
        row("TOWhBinID_FK") = DBNull.Value

        row("Crtd_DT") = Date.Now
        row("Crtd_UId") = AppManager.UserName
        row("LUpd_DT") = Date.Now
        row("LUpd_UId") = AppManager.UserName
    End Sub

    ''
    ''
    '' OutStandingIssues: A faire en Helper - aleblanc 2011-10-26
    ''
    ''
    Public formatType As DevExpress.Utils.FormatType
    ''' <summary>
    ''' use to Build a LOV for our GRID
    ''' </summary>
    ''' <param name="queryName"></param>
    ''' <param name="sqlWhere"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function LOVBuilder(ByVal queryName As String, ByVal sqlWhere As String, ByVal sqlFrom As String, ByVal filter As String) As RepositoryItemGridLookUpEdit
        Dim aEdit As RepositoryItemGridLookUpEdit
        Dim colVisible As Boolean
        Dim hideCol As Boolean
        Dim colname As String
        Dim Column As DevExpress.XtraGrid.Columns.GridColumn
        Dim view As Views.Base.ColumnView

        Using mq As MetaQuery = MetaQuery.GetMetaQueryByName(queryName)
            If Not String.IsNullOrWhiteSpace(sqlWhere) Then
                mq.SQLWhere = sqlWhere
            End If
            '

            'ICI Le DataField filter appliquer quand on a la securité
            If Not String.IsNullOrWhiteSpace(sqlFrom) Then
                mq.SQLFrom = String.Concat(mq.SQLFrom, sqlFrom)
            End If

            If Not String.IsNullOrWhiteSpace(filter) Then
                mq.SQLWhere = String.Concat(mq.SQLWhere, filter)
            End If
            '
            mq.FillResultQuery()
            aEdit = New RepositoryItemGridLookUpEdit() With {.DataSource = mq.ResultQuery, .DisplayMember = mq.ResultQuery.Columns(CommonExt.Base.AppManager.ApplicationOption.DescriptionColumnIndex).ToString, .ValueMember = mq.GUIDField, .PopupFormSize = New Size(800, 300), .NullText = "(none)"}
            aEdit.View.HorzScrollVisibility = Views.Base.ScrollVisibility.Always
            aEdit.View.OptionsView.ColumnAutoWidth = False
            aEdit.View.OptionsView.ShowAutoFilterRow = True
            aEdit.View.OptionsBehavior.ReadOnly = True
            view = aEdit.View
            view.Columns.Clear()
            For Each row As DataRow In mq.MetaColumns.Rows
                colname = row("ColumnName").ToString
                hideCol = False
                If AppManager.ApplicationOption.DescriptionColumnIndex = 2 Then
                    If colname.EndsWith("_ALT") Then
                        hideCol = True
                    End If
                Else
                    If colname.EndsWith("_PRM") Then
                        hideCol = True
                    End If
                End If
                If hideCol = False Then
                    colVisible = CBool(IIf(CInt(row("pVisible")) = 0, False, True))
                    Column = view.Columns.AddField(colname)
                    Column.VisibleIndex = CInt(row("pOrder"))
                    Column.Caption = row("pCaption").ToString
                    Column.Visible = colVisible
                    Column.Width = CInt(row("pWidth"))
                    Column.ToolTip = row("pToolTip").ToString

                    formatType = GetFormatType(row)
                    '
                    Column.DisplayFormat.FormatType = formatType
                    Column.DisplayFormat.FormatString = row("pNumberFormat").ToString
                End If
            Next
            aEdit.DataSource = mq.ResultQuery
            Return aEdit
        End Using
    End Function

    Public Function GetFormatType(ByVal arow As DataRow) As DevExpress.Utils.FormatType
        If (Not String.IsNullOrEmpty(arow("pNumberFormatID_FK").ToString)) Then
            Select Case CType(arow("pNumberFormatID_FK"), Guid)

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_CURRENCY, CommonExt.Base.IdRepository.META_NUMBERFORMAT_GENERALNUMBER, CommonExt.Base.IdRepository.META_NUMBERFORMAT_PERCENT, CommonExt.Base.IdRepository.META_NUMBERFORMAT_STANDARD, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SCIENTIFIC
                    Return DevExpress.Utils.FormatType.Numeric

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_GENERALDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_LONGDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_LONGTIME, CommonExt.Base.IdRepository.META_NUMBERFORMAT_MEDIUMDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_MEDIUMTIME, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SHORTDATE, CommonExt.Base.IdRepository.META_NUMBERFORMAT_SHORTTIME
                    Return DevExpress.Utils.FormatType.DateTime

                Case CommonExt.Base.IdRepository.META_NUMBERFORMAT_FORMATEXPR
                    Return DevExpress.Utils.FormatType.Custom

                Case Else
                    Return DevExpress.Utils.FormatType.None
            End Select
        Else
            Return DevExpress.Utils.FormatType.None
        End If
    End Function
    ''
    ''
    '' OutStandingIssues: FIN A faire en Helper - aleblanc 2011-10-26
    ''
    ''
End Class
